<?php

// 广告位表

namespace app\console\model;
use think\Db;
use think\Model;
use think\Request;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Session;
use think\Hook;

class Order extends Model
{
    // 模型事件
    protected static function init()
    {

        self::event('before_write', function ($data) {
            $member_id = Db::name('member')->where('username',$data->username)->value('id');
            if(empty($member_id)) fail('会员没有注册过');
            $t = explode('/',$data->interval_timer);
            $status = Request::instance()->param('status');
            if($status == 1){
                if(Request::instance()->param('amount') <= 0){
                    fail('付费寄存金额必须大于0');
                }
                $data->order_price = Request::instance()->param('amount');
            }else{
                $data->order_price = 0;
                $data->amount = 0;
            }
            $data->order_type = 2;
            $data->service_type_id = 3;
            $data->member_id = $member_id;
            $data->create_time = date('Y-m-d H:i:s');
            $shop_id = Db::name('shop_create')->where('manage_id',Session::get('manage_id'))->value('shop_id');
            $auding_status = Session::get('auding_status');
            if($auding_status == 1){
                $data->shop_id = $shop_id = \session('shop_id');
            }else{
                $data->shop_id = Request::instance()->param('shop_id');
            }
            $data->is_read = 1;
            $data->code = 2;
            $data->single_id = 11;
            $data->single_parent_id = 4;
            $data->shop_order_number = $data->order_number;
            $data->start_time = $t[0];
            $data->end_time = $t[1];
            if(count(Request::instance()->param('picArr/a')) > 2){
                fail('图片最多上传2张');
            }
            $data->multi_pic = (serialize(Request::instance()->param('picArr/a')));// 组图
            auding($data);

        });
    }

    function getIsCertificationTextAttr($value,$data){
        $status = ['1' => '未认证','2' => '已认证',3 => '等待认证','0' => '-'];
        if(empty($data['is_certification'])) $data['is_certification'] = 0;
        return $status[$data['is_certification']];
    }

    public function getorderOnlineTextAttr($value, $data)
    {
        $status = [1 => '线上订单', 2 => '线下订单',0 => '-'];
        return $status[$data['order_online']];
    }

    public function getisFreeOrderTextAttr($value, $data)
    {
        $status = [1 => '-', 2 => '免费订单',0 => '-'];
        return $status[$data['is_free_order']];
    }

    public function getAudingStatusTextAttr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核通过',0 => '-',3 => '审核失败'];
        return $status[$data['auding_status']];
    }

    public function getfinanceOrderTextAttr($value, $data)
    {

        $status = [1 => '业务员订单', 2 => '二维码订单', 3 => '门店订单', 4 => '普通订单','0' => '-'];
        return $status[$data['finance_order']];
    }



}